﻿Public Class QLMonHoc

    Dim dtProvider As New MonHocDAO()
    Private Sub QLMonHoc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvMonHoc.DataSource = MonHocDAO.GetData()
        LayDSMH()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim chuoiLoi As String = KiemTra()
        If (String.Compare(chuoiLoi, "", True) = 0) Then
            Dim bangMonHoc As DataTable = MonHocDAO.LayCauTrucBang()
            Dim mh As DataRow = bangMonHoc.NewRow()
            mh("MaMonHoc") = txtMaMonHoc.Text.Trim()
            mh("TenMonHoc") = txtTenMonHoc.Text.Trim()
            mh("NgayBatDau") = dtpNgayBatDau.Value
            mh("NgayKetThuc") = dtpNgayKetThuc.Value
            mh("SoTinChi") = Integer.Parse(txtSoTinChi.Text.Trim())

            bangMonHoc.Rows.Add(mh)
            MonHocDAO.CapNhat(bangMonHoc)
            LayDSMH()
            MessageBox.Show("Thêm thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(chuoiLoi, "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        dgvMonHoc.Rows.Remove(dgvMonHoc.CurrentRow)
        Dim bangSinhVien As DataTable = dgvMonHoc.DataSource
        MonHocDAO.CapNhat(bangSinhVien)
        LayDSMH()
        MessageBox.Show("Xóa thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim chuoiLoi As String = KiemTra()
        If (String.Compare(chuoiLoi, "", True) = 0) Then
            Dim bangMonHoc As DataTable = dgvMonHoc.DataSource
            Dim mh As DataRow = bangMonHoc.Rows(dgvMonHoc.CurrentRow.Index)
            mh.BeginEdit()
            mh("MaMonHoc") = txtMaMonHoc.Text.Trim()
            mh("TenMonHoc") = txtTenMonHoc.Text.Trim()
            mh("NgayKetThuc") = dtpNgayKetThuc.Value
            mh("NgayBatDau") = dtpNgayBatDau.Value
            mh("SoTinChi") = Integer.Parse(txtSoTinChi.Text.Trim())
            mh.EndEdit()
            MonHocDAO.CapNhat(bangMonHoc)
            LayDSMH()
            MessageBox.Show("Sửa thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(chuoiLoi, "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim chuoiLoi As String = KiemTra()
        If (String.Compare(chuoiLoi, "", True) = 0) Then
            Dim bangMonHoc As DataTable = MonHocDAO.LayCauTrucBang()
            Dim mh As DataRow = bangMonHoc.NewRow()
            mh("MaMonHoc") = txtMaMonHoc.Text.Trim()
            mh("TenMonHoc") = txtTenMonHoc.Text.Trim()
            mh("NgayKetThuc") = dtpNgayKetThuc.Value
            mh("NgayBatDau") = dtpNgayBatDau.Value
            mh("SoTinChi") = Integer.Parse(txtSoTinChi.Text.Trim())

            bangMonHoc.Rows.Add(mh)
            MonHocDAO.CapNhat(bangMonHoc)
            LayDSMH()
            MessageBox.Show("Update thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(chuoiLoi, "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        If (MessageBox.Show("Bạn có chắc chắn muốn thoát hay không?", "Thoát khỏi chương trình", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
            Me.Close()
            Global.System.Windows.Forms.Application.Exit()
        End If
    End Sub

    Private Sub dgvMonHoc_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvMonHoc.SelectionChanged
        Dim dong As DataGridViewRow = dgvMonHoc.CurrentRow
        If (dong Is Nothing) Then
            Exit Sub
        End If
        txtMaMonHoc.Text = dong.Cells("MaMonHoc").Value.ToString()
        txtTenMonHoc.Text = dong.Cells("TenMonHoc").Value.ToString()
        dtpNgayKetThuc.Value = dong.Cells("NgayKetThuc").Value
        dtpNgayBatDau.Value = dong.Cells("NgayBatDau").Value
        txtSoTinChi.Text = dong.Cells("SoTinChi").Value.ToString()
    End Sub


    Public Function KiemTra() As String
        Dim chuoiLoi As String = ""
        If (String.Compare(txtMaMonHoc.Text.Trim(), "", True) = 0) Then
            chuoiLoi &= "MaMonHoc phải khác rỗng" & vbCrLf
        End If

        If (String.Compare(txtTenMonHoc.Text.Trim(), "", True) = 0) Then
            chuoiLoi &= "Tên môn học phải khác rỗng" & vbCrLf
        End If
        Return chuoiLoi
    End Function

    Public Sub LayDSMH()
        dgvMonHoc.Columns(0).Visible = True
        dgvMonHoc.Columns(1).Visible = True
        dgvMonHoc.Columns(2).Visible = True
        dgvMonHoc.Columns(3).DefaultCellStyle.Format = "dd/MM/yyyy"
        dgvMonHoc.Columns(4).DefaultCellStyle.Format = "dd/MM/yyyy"

        dgvMonHoc.Columns(1).HeaderText = "Tên môn học"
        dgvMonHoc.Columns(2).HeaderText = "Số tín chỉ"
        dgvMonHoc.Columns(3).HeaderText = "Ngày bắt đầu"
        dgvMonHoc.Columns(4).HeaderText = "Ngày kết thức"

        For i As Integer = 0 To dgvMonHoc.Columns.Count - 1
            dgvMonHoc.Columns(i).ReadOnly = True
        Next
    End Sub
End Class